﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Da = VLUNewsData;
using DaMo = VLUNewsData.EntityDataModel;
using BuUt = VLUNewsBusiness.Utilities;
using BuRU = VLUNewsBusiness.VLURolesAndUsers;
using System.Collections;
using System.Data.Objects;

namespace VLUNewsBusiness.VLUNews
{
    public class Comment
    {
        #region Base members
        private static Comment _Instance = null;
        public static Comment Instance()
        {
            if (_Instance == null) _Instance = new Comment();
            return _Instance;
        }

        public static void Release()
        {
            if (_Instance != null)
                GC.SuppressFinalize(_Instance);
        }
        #endregion Base members

        #region Save
        public int Save(int pHtmlContentID, string pComment, int pUserID)
        {
            int result = -1;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                DaMo.VluNewsComment model = new DaMo.VluNewsComment();
                ent.VluNewsComments.AddObject(model);
                model.HtmlContentID = pHtmlContentID;
                model.Comment = pComment;
                model.CreatedByUserID = pUserID;
                model.CreatedOnDate = DateTime.Now;
                model.LastModifiedByUserID = pUserID;
                model.LastModifiedOnDate = DateTime.Now;

                result = ent.SaveChanges();
            }// using.
            return result;
        }
        #endregion Save

        #region Get
        public IList GetsByHtmlContentID(int pHtmlContentID)
        {
            IList result = null;
            IQueryable<DaMo.VluNewsComment> query;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                query = ent.VluNewsComments.Where(p => p.HtmlContentID == pHtmlContentID).OrderByDescending(p => p.LastModifiedOnDate);
                if (query.Count() > 0)
                {
                    result = query.ToList().Select(p =>
                    new
                    {
                        p.Comment,
                        OwnerName = p.Modifier.Username,
                    }).ToList();
                }
            }
            return result;
        }
        #endregion Get
    }
}
